package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.tsktaskhistory.TskTaskHistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.tsktaskhistory.TskTaskHistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.tsktaskhistory.TskTaskHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.tsktaskhistory.TskTaskHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.tsktaskhistory.TskTaskHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.tsktaskhistory.TskTaskHistoryVo;
import com.bwda.dsrs.filecenter.service.TskTaskHistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/tsktaskhistory")
@Api(value = "/tsktaskhistory", tags = "定时任务执行历史记录表")
public class TskTaskHistoryController extends BaseController {

	@Autowired
	private TskTaskHistoryService tskTaskHistoryService;

	@ApiOperation(value = "查询定时任务执行历史记录表",notes = "查询定时任务执行历史记录表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TskTaskHistoryVo> query(@ApiParam(value = "历史记录id", required = true)@RequestParam Integer taskHistoryId) throws BusinessServiceException {
		TskTaskHistoryPo po = tskTaskHistoryService.queryWithValid(taskHistoryId);
		TskTaskHistoryVo vo = CopyUtil.transfer(po, TskTaskHistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询定时任务执行历史记录表数量",notes = "查询定时任务执行历史记录表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TskTaskHistoryQueryForm form) throws BusinessServiceException {
		TskTaskHistoryCondition condition = this.getConditionByQueryForm(form);
		int count = tskTaskHistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询定时任务执行历史记录表列表",notes = "查询定时任务执行历史记录表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TskTaskHistoryVo>> queryList(@ModelAttribute@Valid TskTaskHistoryQueryForm form) throws BusinessServiceException {
		TskTaskHistoryCondition condition = this.getConditionByQueryForm(form);
		List<TskTaskHistoryPo> poList = tskTaskHistoryService.queryList(condition);
		List<TskTaskHistoryVo> voList = CopyUtil.transfer(poList, TskTaskHistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询定时任务执行历史记录表列表(带分页)",notes = "查询定时任务执行历史记录表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TskTaskHistoryVo>> queryPageList(@ModelAttribute@Valid TskTaskHistoryQueryForm form) throws BusinessServiceException {
		TskTaskHistoryCondition condition = this.getConditionByQueryForm(form);
		List<TskTaskHistoryVo> voList = new ArrayList<>();
		int count = tskTaskHistoryService.queryCount(condition);
		if (count > 0) {
			List<TskTaskHistoryPo> poList = tskTaskHistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, TskTaskHistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "修改定时任务执行历史记录表",notes = "修改定时任务执行历史记录表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TskTaskHistoryUpdateForm form) throws BusinessServiceException {
		TskTaskHistoryPo po = CopyUtil.transfer(form, TskTaskHistoryPo.class);
		tskTaskHistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除定时任务执行历史记录表",notes = "删除定时任务执行历史记录表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "历史记录id", required = true)@RequestParam Integer taskHistoryId) throws BusinessServiceException {
		tskTaskHistoryService.delete(taskHistoryId);
		return getSuccessResult();
	}

	/**
	 * TskTaskHistoryQueryForm转换为TskTaskHistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private TskTaskHistoryCondition getConditionByQueryForm(TskTaskHistoryQueryForm form) {
		TskTaskHistoryCondition condition = CopyUtil.transfer(form, TskTaskHistoryCondition.class);
		return condition;
	}

}